package com.view.cn.mvprxjava.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import com.view.cn.mvprxjava.R;


/**
 * fragment的工具类
 * Created by zxp19920626 on 2016/5/20.
 */
public class FragmentUtils {

    public static final int ADD = 1;
    public static final int REMOVE = 2;
    public static final int REPLACE = 3;
    public static final int HIDE = 4;
    public static final int SHOW = 5;

    public static void replaceFragment(AppCompatActivity activity, Fragment fragment, String tag) {
        replaceFragment(activity, fragment, tag, false);
    }

    public static void replaceFragment(AppCompatActivity activity, Fragment fragment, String tag, boolean needPop) {
        replaceFragment(activity, fragment, tag, false, true);
    }

    public static void replaceFragment(AppCompatActivity activity, Fragment fragment, String tag, boolean needPop, boolean isAddBackStack) {
        replaceFragment(activity, fragment, tag, false, true, R.id.contanier, REPLACE);
    }

    public static void replaceFragment(AppCompatActivity activity, Fragment fragment, String tag, boolean needPop, boolean isAddBackStack, int layoutId, int antionType) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        if (needPop) {
            fragmentManager.popBackStack();
        }
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
//        //Fragment切换的自定义动画
//        fragmentTransaction.setCustomAnimations(R.anim.in_from_right,
//                R.anim.out_from_left,
//                R.anim.in_from_left,
//                R.anim.out_from_right);
        switch (antionType) {
            case ADD:
                fragmentTransaction.add(layoutId, fragment, tag);
                break;
            case REMOVE:
                fragmentTransaction.remove(fragment);
                break;
            case REPLACE:
                fragmentTransaction.replace(layoutId, fragment, tag);
                break;
            case HIDE:
                fragmentTransaction.hide(fragment);
                break;
            case SHOW:
                fragmentTransaction.show(fragment);
                break;
            default:
                break;
        }
        //标准动画（自带）
//        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);

        if (isAddBackStack) {
            fragmentTransaction.addToBackStack(null);
        }
        fragmentTransaction.commitAllowingStateLoss();
    }
}
